package com.zh.tea.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zh.tea.entity.Tea;
import com.zh.tea.service.TeaService;
import com.zh.tea.service.impl.TeaServiceImpl;

import net.sf.json.JSONObject;


/**
 * Servlet implementation class TeaServlet
 */
@WebServlet("/TeaServlet")
public class TeaServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public TeaServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html; charset=utf-8");
		PrintWriter out=response.getWriter();
		HttpSession session= request.getSession();
		
		//创建Tea的serivce对象
		TeaService teas=new TeaServiceImpl();
		String opt=request.getParameter("opt");
		System.out.println(opt);
		if("add".equals(opt)){
			String  typeid=null;
			String  brandid=null;
			String  teano=null;
			String  name=null;
			String  price=null;
			String  spec=null;
			String  packspec=null;
			String  licenseno=null;
			String  season=null;
			//tech text  producedate bzq pic1 shortinfo
			String tech=null;
			String bzq=null;
			String pic1=null;
			String pic2=null;
			String pic3=null;
			String shortinfo=null;
			
			
			//新建一个FileItrm的工厂类分析input标签
			FileItemFactory factory=new DiskFileItemFactory();
			//创建input框分析器
			ServletFileUpload fileUpload=new ServletFileUpload(factory);
			//分析request请求
			try {
				List<FileItem> list=fileUpload.parseRequest(request);
				for(FileItem item:list){
					if(item.isFormField()){
						if("typeid".equals(item.getFieldName())){
							typeid=item.getString("utf-8");
						}else if("brandid".equals(item.getFieldName())){
							brandid=item.getString("utf-8");
						}else if("teano".equals(item.getFieldName())){
							teano=item.getString("utf-8");
						}else if("name".equals(item.getFieldName())){
							name=item.getString("utf-8");
						}else if("price".equals(item.getFieldName())){
							price=item.getString("utf-8");
						}else if("spec".equals(item.getFieldName())){
							spec=item.getString("utf-8");
						}else if("packspec".equals(item.getFieldName())){
							packspec=item.getString("utf-8");
						}else if("licenseno".equals(item.getFieldName())){
							licenseno=item.getString("utf-8");
						}else if("".equals(item.getFieldName())){
							typeid=item.getString("utf-8");
						}else if("".equals(item.getFieldName())){
							typeid=item.getString("utf-8");
						}
					}
				}
				
			} catch (FileUploadException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
					
			
			
			
		}else if("sel".equals(opt)){
			String pageNo=request.getParameter("pageno");
//			String pageSize=request.getParameter("pagesize");
			
			PageInfo<Tea> paget= teas.getTea(null, 1, 5);
			session.setAttribute("paget", paget);
			response.sendRedirect("teashoplist.jsp");
			
		}else if("toupd".equals(opt)){
			String teaid=request.getParameter("teaid");
			Tea tea=teas.getTeaById(Integer.parseInt(teaid));
			request.setAttribute("tea", tea);
			request.getRequestDispatcher("../admin/UpadateGoods.jsp").forward(request, response);
			
		}
		//显示所有茶商品
		else if("showAll".equals(opt)){
					
			int currentNumber = Integer.parseInt(request.getParameter("page")==null?"1":request.getParameter("page"));//页码
	        String currentPage = request.getParameter("limit") == null ? "1" : request.getParameter("limit");//大小
			
	        PageInfo<Tea> paget= teas.getTea(null, currentNumber, Integer.parseInt(currentPage));

				JSONObject jsonObject = new JSONObject();
		        jsonObject.put("code", 0);
		        jsonObject.put("msg", "");
		        jsonObject.put("count", paget.getTotal());
		        jsonObject.put("data", paget.getList());
			
					
					String json=jsonObject.toString();
					out.print(json);
				}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
